package com.zzptc.easyshop.dao;

import com.zzptc.easyshop.db.IDB;
import com.zzptc.easyshop.db.MysqlDB;
import com.zzptc.easyshop.vo.Message;
import com.zzptc.easyshop.vo.VO;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class MessageDao implements IDAO {
    @Override
    public boolean insert(VO vo) throws SQLException {
        IDB db=new MysqlDB();
        Connection conn=db.getConn();
        PreparedStatement ps = conn.prepareStatement("insert into Easybuy_Comment(ec_content,ec_create_time,ec_title,ec_nick_name  ) value (?,?,?,?)");
        Message message=(Message) vo;
        ps.setString(1,message.getEc_content());
        ps.setString(2,message.getEc_create_time());
        ps.setString(3,message.getEc_title());
        ps.setString(4,message.getEc_nick_name());
        int i=ps.executeUpdate();
        return i>0;
    }

    @Override
    public boolean login(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean delete(VO vo) throws SQLException {
        return false;
    }

    @Override
    public boolean updata(VO vo) throws SQLException {
        return false;
    }

    @Override
    public List getAll() throws SQLException {
        IDB db=new MysqlDB();
        Connection conn=null;
        PreparedStatement ps=null;
        ResultSet rs=null;
        conn=db.getConn();
        ps=conn.prepareStatement("select * from Easybuy_Comment");
        rs=ps.executeQuery();
        List list=new ArrayList();
        while (rs.next()){
            Message message=new Message();
            message.setEc_id(rs.getInt(1));
            message.setEc_reply(rs.getString(2));
            message.setEc_content(rs.getString(3));
            message.setEc_reply_time(rs.getString(4));
            message.setEc_nick_name(rs.getString(5));
            message.setEc_title(rs.getString(6));
            list.add(message);
        }
        db.closeAll(conn,ps,rs);
        return list;
    }

    public static void main(String[] args) {
        MessageDao messageDao=new MessageDao();
        try {
            System.out.println(messageDao.getAll().size());
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
